<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>

    <title>找房无忧-我的页面</title>
    <link rel="stylesheet" href="css/reset.css"/>
    <link rel="stylesheet" href="css/my.css"/>
    <script src="https://code.jquery.com/jquery-3.4.1.js?unf=3545" rel="script" type="text/javascript"></script>
    <script src="js/public.js" rel="script" type="text/javascript"></script>
</head>
<body>
<!--头部-->
<section class="head">
    <img class="bg" src="image/my/bg1.png" alt=""/>
    
    <img class="logo" id="imgSrc" src="image/my/urse2.png" alt=""/>
    <div id="username">
        <a class="login" id="login-button" onclick="login()">登录</a>
    </div>
</section>

<section class="userList">
    <ul>
	<a href="myHouse.html">
		<li>
            <img src="image/my/icon1.png" alt=""/>
            我关注的房源
        </li>
	
	</a>
        
        
        <li>
            <img src="image/my/icon4.png" alt=""/>
            <a href="#">我的看房记录</a>
        </li>
    </ul>
</section>


<script src="js/rem.js"></script>
<script>
    function login() {
        //回调页面即是当前页面
        var redirectUrl = encodeURIComponent(location.href);
        location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx397e5e25b8bce662&redirect_uri=" + redirectUrl + "&response_type=code&scope=snsapi_userinfo"
    }

    /**
     *
     *  获取URL中的参数
     * */
    function getUrlParam(url,name){
        var pattern = new RegExp("[?&]"+name+"\=([^&]+)", "g");
        var matcher = pattern.exec(url);
        var items = null;
        if(null != matcher){
            try{
                items = decodeURIComponent(decodeURIComponent(matcher[1]));
            }catch(e){
                try{
                    items = decodeURIComponent(matcher[1]);
                }catch(e){
                    items = matcher[1];
                }
            }
        }
        return items;
    }

    /*var userCookie = getCookie("user");
    if (userCookie != null) {
        $("#username").html("<a class=\"login\">"+userCookie.username+"</a>");
        $("#imgSrc").attr("src" , userCookie.imgSrc);
    }*/

    //获取code
    var code = getUrlParam(location.href , "code");
    if (code != null) {
        //有code
        $.ajax({
            url: baseUrl + "/tenement/user/login?code=" + code,
            dataType:"json",
            success:function (res) {
                $("#username").html("<a class=\"login\">"+res.data.username+"</a>");
                $("#imgSrc").attr("src" , res.data.imgSrc);
                //存放到cookie当中
                addCookie("user" , res.data);
            },
            error:function () {
                alert("错误");
            }
        })
    }
</script>
</body>
</html>